/* -----------------
* brief
* 1. this is code gen by tools
* 2. mvc code are driver by the model defined with the same filename,
	 for example:
	 there is a Xmodel struct define in xmodel.go file, after the script run (lego model-mvc module_name xmodel)
     it will auto create the code under the module's path
	 m --> xmodel.go
	 v --> view
	 c --> xmodel_ctrl
* 3. you should modify this code if it doesn't satisfy your purpose
*/

package controller

import (
	"github.com/mjiulee/lego"
	"github.com/mjiulee/lego/utils"
	"github.com/valyala/fasthttp"

	. "saas_sys_base/modules/eshop/models"
	. "saas_sys_base/modules/eshop/service"
)

type ShopCateCtrl struct {
	service ShopCateService
}

func init() {
	vc := new(ShopCateCtrl)
	_ctrlList = append(_ctrlList, vc)
}

func (vc *ShopCateCtrl) CtrlInfo() string {
	return "ShopCateCtrl"
}

func (vc *ShopCateCtrl) RoutInit() {
	// 列表页面
	lego.GET("/admin/ShopCate/listpage", vc.ListPage, lego.LEGO_SESSION_TYPE_ADMIN, true)
	lego.GET("/admin/ShopCate/listjson", vc.ListJson, lego.LEGO_SESSION_TYPE_ADMIN, true)
	// 编辑页面
	lego.GET("/admin/ShopCate/editpage", vc.GetEditPage, lego.LEGO_SESSION_TYPE_ADMIN, true)
	lego.POST("/admin/ShopCate/editpage", vc.PostEditPage, lego.LEGO_SESSION_TYPE_ADMIN, true)
	// 删除
	lego.POST("/admin/ShopCate/del", vc.DelRecode, lego.LEGO_SESSION_TYPE_ADMIN, true)

	lego.GET("/admin/ShopCate/getcates", vc.GetCates, lego.LEGO_SESSION_TYPE_ADMIN, false)

	lego.APIGET("/api/ShopCate/getcates", vc.GetCates, false)
}

/* 列表接口
* params:
  ---
*/
func (vc *ShopCateCtrl) ListJson(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}
	response := make(map[string]interface{})
	code := 0
	msg := ""
	for {
		// 表单校验
		type ReqParams struct {
			Keyword string // 关键字查询
			Page    int    `vd:"$>=0"` // 分页
			PSize   int    `vd:"$>0"`  // 分页大小
			Field   string //排序字段
			Order   string //排序  asc desc
		}
		req := ReqParams{
			Keyword: string(ctxExt.Peek("keyword")),
			Page:    utils.BytesToInt(ctxExt.Peek("page")),
			PSize:   utils.BytesToInt(ctxExt.Peek("limit")),
			Field:   string(ctxExt.Peek("field")),
			Order:   string(ctxExt.Peek("order")),
		}
		if err := lego.Validate(req); err != nil {
			code = 100
			msg = err.Error()
			break
		}

		var sort string = "t1.id asc"
		if len(req.Field) > 0 {
			sort = req.Field + " " + req.Order
		}

		// 业务查询
		if list, total, err := vc.service.ListShopCate(req.Keyword, sort, req.Page, req.PSize); err != nil {
			lego.LogError(err)
		} else {
			response["count"] = total
			response["data"] = list
		}
		break
	}

	response["code"] = code
	response["msg"] = msg
	ctxExt.JSON(200, response)
	return
}

/* 添加数据记录
* params:
  ---
*/
func (vc *ShopCateCtrl) ListPage(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}
	data := make(map[string]interface{})
	for {
		break
	}
	ctxExt.HTML("eshop/view/ShopCate/list.html", data)
	return
}

/* 添加数据记录
* params:
  ---
*/
func (vc *ShopCateCtrl) GetEditPage(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}
	data := make(map[string]interface{})
	for {
		type ReqParams struct {
			Id int64 // 记录id，为空则新建，否则为编辑
		}
		req := ReqParams{
			Id: utils.BytesToInt64(ctxExt.Peek("id")),
		}
		if err := lego.Validate(req); err != nil {
			break
		}
		target := vc.service.GetShopCateById(req.Id)
		data["detail"] = target
		break
	}
	ctxExt.HTML("eshop/view/ShopCate/edit.html", data)
	return
}

/* 数据编辑
* params:
  ---
*/
func (vc *ShopCateCtrl) PostEditPage(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}
	response := make(map[string]interface{})
	data := make(map[string]interface{})

	code := 0
	msg := ""

	for {
		// 表单校验
		type ReqParams struct {
			//#ModelEditFormDefined#
			Id       int64  `vd:""`
			CateName string `vd:""`
		}
		req := ReqParams{
			//#ModelEditFormFiled#
			Id:       utils.BytesToInt64(ctxExt.Peek("id")),
			CateName: string(ctxExt.Peek("cate_name")),
		}
		if err := lego.Validate(req); err != nil {
			code = 100
			msg = err.Error()
			break
		}

		// 业务处理
		recode := vc.service.GetShopCateById(req.Id)
		if recode == nil {
			// 新建
			recode := new(ShopCate)
			//#ModelFiledMapper#'
			recode.CateName = req.CateName

			if false == vc.service.SaveShopCate(recode) {
				code = 100
				msg = "false"
				break
			}
		} else {
			//#ModelFiledMapper#
			recode.CateName = req.CateName

			if false == vc.service.UpdateShopCate(recode) {
				code = 100
				msg = "false"
				break
			}
		}
		break
	}
	response["code"] = code
	response["msg"] = msg
	response["data"] = data
	ctxExt.JSON(200, response)
	return
}

/* 删除数据记录
* params:
  ---
*/
func (vc *ShopCateCtrl) DelRecode(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}
	response := make(map[string]interface{})
	data := make(map[string]interface{})

	code := 0
	msg := ""
	for {
		// 表单校验
		type ReqParams struct {
			Id int64 `vd:"$>0"` // 记录id
		}
		req := ReqParams{
			Id: utils.BytesToInt64(ctxExt.Peek("id")),
		}
		if err := lego.Validate(req); err != nil {
			break
		}
		// 业务删除
		target := vc.service.GetShopCateById(req.Id)
		if target == nil {
			code = 100
			msg = "记录不存在"
			break
		}

		if false == vc.service.DelShopCateById(target.Id) {
			code = 100
			msg = "操作失败"
			break
		}
		break
	}

	response["code"] = code
	response["msg"] = msg
	response["data"] = data
	ctxExt.JSON(200, response)
	return
}

func (vc *ShopCateCtrl) GetCates(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}
	cateList := vc.service.GetShopCates()

	response := make(map[string]interface{})
	response["code"] = 0
	response["msg"] = ""
	response["data"] = cateList
	ctxExt.JSON(200, response)
	return
}
